<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" :validatorRules="validatorRules">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="字段类型 ">
          <a-radio-group name="type" v-decorator="['type', { initialValue: 1 }]">
            <a-radio :value="1">商铺</a-radio>
            <a-radio :value="2">商品</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="字段名称">
          <a-input placeholder="请输入名称" v-decorator="['name', validatorRules.name]" />
        </a-form-item>
        <a-form-item label="类型图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-upload
            action="/mall-serve/v1/sys/common/uploadImage"
            name="file"
            listType="picture-card"
            @preview="handleShopTypePreview"
            @change="handleShopTypeChange"
            :fileList="picList"
            class="avatar-uploader"
            :showUploadList="true"
          >
            <div v-if="picList.length < 1">
              <a-icon type="plus" />
              <div class="ant-upload-text">上传</div>
            </div>
          </a-upload>
          <a-modal :visible="previewVisible" :footer="null" @cancel="handlePicCancel">
            <img alt="example" style="width: 100%" :src="previewImage" />
          </a-modal>
        </a-form-item>
        <!-- <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="上一级字典项">
          <a-select
            showSearch
            :value="value"
            placeholder="input search text"
            style="width: 200px"
            :defaultActiveFirstOption="false"
            :showArrow="false"
            :filterOption="false"
            @search="handleSearch"
            @change="handleChange"
            :notFoundContent="null"
          >
            <a-select-option v-for="d in data" :key="d.value">{{d.text}}</a-select-option>
          </a-select>
        </a-form-item> -->
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
          <a-input placeholder="请输入备注" v-decorator="['note', {}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from 'moment'

export default {
  name: 'shopTypeModal',
  data() {
    return {
      imageDownloadUri: '/mall-serve/v1/sys/common/getImageByUri?imageUri=',
      picList: [],
      picture: '',
      previewImage: '',
      previewPicImage: '',
      previewVisible: false,
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        type: { rules: [{ required: true, message: '请选择类型' }] },
        name: { rules: [{ required: true, message: '请输入名称!' }] }
      },
      url: {
        add: '/shopType/add',
        edit: '/shopType/edit'
      }
    }
  },
  created() {},
  methods: {
    handlePicCancel() {
      this.previewVisible = false
    },
    handleShopTypePreview(file) {
      this.previewPicImage = file.url || file.thumbUrl
      this.previewPicVisible = true
    },
    handleShopTypeChange({fileList}) {
      console.log(fileList)
      this.picList = fileList
    },
    add() {
      this.form.resetFields()
      this.picList = []
      this.visible = true
    },
    edit(record) {
      this.visible = true
      this.picList = []
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'type',
            'picture',
            'name', 
            'note'))
        console.log(this.model)
        if (this.model.picture != null && this.model.picture != undefined) {
          console.log(this.model.picture)
          this.picList.push({
            uid: Math.random(),
            name: this.model.picture,
            url: this.imageDownloadUri + this.model.picture,
            status: 'done',
            type: 'image/*'
          })
        }
        this.visible = true

        //时间格式化
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          if (!this.model.id) {
            formData.level = 1
          }
          if(that.picList[0].response !=undefined && that.picList[0].response !=null){
             formData.picture = this.picList[0].response.result.imageUri
          }
           
          //时间格式化

          console.log(formData)
          httpAction(httpurl, formData, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>

<style lang="less" scoped></style>
